//
// Ephi - simulation of magnetic fields and particles
// Copyright (C) 2007 Indrek Mandre <indrek(at)mare.ee>
// For more information please see http://www.mare.ee/indrek/ephi/
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along
// with this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <vector>

#include "ephi.hpp"

#define PMAP_BOX 0.1

class MyPlasma : public Plasma
{
public:
  MyPlasma () : Plasma (TREE_FULL)
  {
    qmul = 1e7;
    pmap_box = PMAP_BOX;
    pmap_resolution = 0.01;
    pmap_smooth = 0.02;
    stepmag = 1;

    octree_c0 = vect3d(-pmap_box, -pmap_box, -pmap_box);
    octree_c1 = vect3d(pmap_box, pmap_box, pmap_box);
    octree_resolution = 0.01;

    particle_plane = Screen::PLANE_ZX;

    // we are ready, init plasma
    init();
  }

  void inject ()
  {
    for ( size_t i = 0; i < 500; i++ )
      {
        prec_t x = prec_t_drand() * 0.01 - 0.005;
        prec_t y = prec_t_drand() * 0.01 - 0.005;
        prec_t z = prec_t_drand() * 0.01;
        if ( x * x + y * y > 0.005 * 0.005 )
          {
            i--;
            continue;
          }
        dyn.inject_electron (vect3d(x, y, -pmap_box + z), vect3d(0, 0, 1), 10000);
      }
    printf ("Pot at middle = %e\n", dyn.getPotential (vect3d(0, 0, -pmap_box + 0.05)));
  }
};

int main (int argc, char *argv[])
{
  Ephi::setDebugLevel (Ephi::DEBUG_APP);

  MyPlasma plasma;
  plasma.inject();
  plasma.step();

  size_t n = 0;
  while (1)
    {
      char name[128];
      sprintf (name, "test20_%03d", (int)n);
      plasma.render (name, false);
      for ( size_t c = 0; c < 5; c++ )
        {
          for ( size_t i = 0; i < 10; i++ )
              plasma.step();
          plasma.inject();
        }
      n++;
      if ( n > 100 )
          break;
    }
  
  return 0;
}

